/**
 * Created by lxd on 2017/6/26.
 * 实时报警灯亮功能实现
 */
var alarmTop = {
	init : function(){
		$.ajax({
        	url : appCtx + 'gis/monitor/queryDeploymentAreaInfo.jhtml',
        	dataType : 'json',
        	success : function(data){
        		switch (data.code){
				case "520000":
					$('.logo img').attr('src',appCtx+'resources/images/main/logo_guizhou.png');
					break;
				case "450100":
					$('.logo img').attr('src',appCtx+'resources/images/main/logo_nanning.png');
					break;
				case "131000":
					$('.logo img').attr('src',appCtx+'resources/images/main/logo_langfang.png');
					break;
				case "0":
					$('.logo img').attr('src',appCtx+'resources/images/main/logo_yicheng.png');
					break;
				}
        	}
        });
		/**
		 * 加载音频
		 */
		alarmTop.loadAudio();
		/**
		 * 查询实时报警
		 */
		//alarmTop.hasAlarmJob();
		/**
		 * 关闭报警声音提醒
		 */
		$("#postCheckbox").height($(window).height());
		$(".chagang").click(function(){
			$("#postCheckbox").css('display','block');
			$("#background").css('display','block');
		})
		$(".close").click(function(){
			$("#postCheckbox").css('display','none');
			$("#background").css('display','none')
		});
		// 查岗应答界面赋值
		$('#updateBtn').click(function(){
			
			var checkedIds = $("#tbFindAreaChecks").getCheckedRows();
			if(checkedIds.length<1){
				alert('请选择要应答的数据！');
				return;
			}
			if(checkedIds.length>1){
				alert('只能选择一条数据！');
				return;
			}
			$("#updateAreaCheckForm").css('display','block');
			var url = appCtx + "system/areaCheck/toUpdateAreaCheck.html?id="+checkedIds[0].replace(/row/, "");
			$.post(url, function(data) {			
				var model = data.data;
				if(data==null){
					alert('数据加载失败');
				}else{
					
					$("#checker").val(model.checker);
					$("#check_area_name").val(model.check_area_name);
					$("#check_content").val(model.check_content);
					$("#_id").val(model.id);
					$("#check_time").val(formatDate(model.check_time));
					$("#check_area_id").val(model.check_area_id);
					$("#answer").val(model.answer);
					$("#check_id").val(model.check_id);
					$("#check_type").val(model.check_type);
				}
			}, "json");	
		});
		
		// 提交
		$("#saveAreaChecks").click(function(){
			
			var url = appCtx + "system/areaCheck/updateAreaCheck.html"
			var flag = true;
			var reply_content = $.trim($("#reply_content").val());
			if(reply_content == ""){
				flag = false ;
				alert('回复内容不能为空!');
				return false;
			}
			
			var params = $("#updateAreaCheckForm").serialize();  
			var _id = $("#_id").val();
			var check_time1 = $("#check_time1").val();// 时间格式后台实体转换不了，需单独传入JAVA后台
			if (flag) {
				$.ajax({
					type : 'post',
					async : false,
					url : url,
					data : params,_id:_id,check_time1:check_time1,
					cache : false,
					dataType : "JSON",
					success : function(data) {
						
						if (data.code == "0") {
							alert('操作成功!');
							$('#updateAreaCheckForm').css('display','none');
							// 刷新表格
							$("#tbFindAreaChecks").flexOptions( {
								newp : 1
							}).flexReload();
						} else {
							alert("操作失败!");
							return false;
						}
					},
					error : function(hxr, s, e) {
						alert(3333)
					}
				});
			}
			return false;
		})
	},
	bindVoiceEvent : function(){
		if($("#voice").attr("type") == "0"){
			$("#voice").attr("type","1");
			$("#chatAudio")[0].pause();
			$("#voice").text("开启报警声音");
		}else{
			$("#voice").attr("type","0");
			$("#voice").text("关闭报警声音");
		}
		//将设置回传给后台session
		$.ajax({
	     	   url:appCtx+'alarm/setVoice.html?time=' + new Date().getTime(),
	     	   type:'post',
	     	   contentType: "application/json; charset=utf-8",
	     	   dataType:'json',
	     	   cache:'false',
	     	   data : "{'state':'" + $("#voice").attr("type") + "'}",
	     	   success : function(result){
	     	   }
	    });
	},
	
	loadAudio : function(){
		if(typeof(alarmTop.IEborswer) == "undefined"){
			alarmTop.IEborswer = !!window.ActiveXObject || "ActiveXObject" in window;
		}
		var $audio ;
		if (alarmTop.IEborswer){
			$audio = '<embed id="chatAudio" src="' + basePath + '/resources/wav/Em.wav" autostart="false" hidden="true" loop="false"></embed>';
		}else{
			$audio = $('<audio id="chatAudio">' + 
						'<source src="' + basePath + '/resources/wav/Em.wav" type="audio/mpeg">' + 
			  		   '</audio>');
		}
		$($audio).appendTo('body');
	},
	hasAlarmJob : function(){
		$.ajax({
	     	   url:appCtx+'alarm/hasAlarm.html?time=' + new Date().getTime(),
	     	   type:'GET',
	     	   contentType: "application/json; charset=utf-8",
	     	   dataType:'json',
	     	   cache:'false',
	     	   success : function(result){
					if (result == null){
						return ;
					}
	     		   if(result.data == 0){
	     			    //存在实时报警
		     			$("#alarm_icon").show();
		     			if(result.voice == "0"){
		     				$("#chatAudio")[0].play();
		     			}
	     		   }else{
	     			    $("#alarm_icon").hide();
	     			    $("#chatAudio")[0].pause();
	     		   }
	     		   setTimeout(alarmTop.hasAlarmJob,30000);
	     	   },
				error : function () {
                    setTimeout(alarmTop.hasAlarmJob,30000);
                }
	    });
	},
};
alarmTop.init();

function formatDate(time){
	if(time.length == 0){
		return "";
	}else{
		var now=new Date(parseInt(time)); 
		var year=now.getFullYear(); 
		var month=((now.getMonth() + 1) < 10 ? "0" : "") + (now.getMonth() + 1);
		var date=(now.getDate() < 10 ? "0" : "") + now.getDate(); 
		var hour=(now.getHours() < 10 ? "0" : "") + now.getHours(); 
		var minute=(now.getMinutes() < 10 ? "0" : "") + now.getMinutes(); 
		var second=(now.getSeconds() < 10 ? "0" : "") + now.getSeconds(); 
		return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;   
	}
}
